use crate::controller::Error;
use crate::service::ServiceSet;
use crate::{dto, mq};
use std::sync::Arc;
use crate::controller::event_handler::candidate_path::EventHandler;

pub struct CandidatePathSegmentListController {
    service_set: Arc<ServiceSet>,
    event_handler: EventHandler,
}

impl CandidatePathSegmentListController {
    pub fn new(service_set: Arc<ServiceSet>, mq_client_set: Arc<mq::client::ClientSet>) -> Self {
        Self {
            service_set: service_set.clone(),
            event_handler: EventHandler::new(service_set, mq_client_set.clone()),
        }
    }
    pub async fn add(
        &self,
        request: &dto::candidate_path_segment_list::AddRequest,
    ) -> Result<(), Error> {
        self.service_set
            .candidate_path_segment_list_manager
            .add(request)
            .await?;
        self.event_handler
            .spawn_handle_setup_event(request.candidate_path_id.as_str());
        Ok(())
    }

    pub async fn delete(
        &self,
        request: &dto::candidate_path_segment_list::DeleteRequest,
    ) -> Result<(), Error> {
        self.service_set
            .candidate_path_segment_list_manager
            .delete_by_filter(&dto::core::candidate_path_segment_list::Filter {
                candidate_path_id: Some(request.candidate_path_id.clone()),
                segment_list_id: Some(request.segment_list_id.clone()),
            })
            .await?;

        self.event_handler
            .spawn_handle_setup_event(request.candidate_path_id.as_str());
        Ok(())
    }
}